<?php
/**
 * UserView
 * Handles views and events for the UserController.
 */
class UserView
{
	/**
	 * Shows a of all the users in the array.
	 *
	 * @param Array $users An array with User-objects.
	 * @return String Presents a table of users with actions like Delete, Change password.
	 */
	public function doUserList( $users )
	{
		if( !$users )
		{
			$xhtml = 'No users..';
		}
		else
		{
			$xhtml = '<table>';
		
			$xhtml .= '<tr><td bgcolor="#ccc"><strong>UserId</strong></td><td bgcolor="#ccc"><strong>Username</strong></td><td bgcolor="#ccc"><strong>Email</strong></td><td bgcolor="#ccc">Action</td></tr>';
		
			for( $i = 0; $i < count( $users ); $i++ )
			{
				$xhtml .= '<tr valign="top"><td>'.$users[$i]->GetUserId().'</td><td>'.$users[$i]->GetUsername().'</td><td>'.$users[$i]->GetEmail().'</td><td><form action="" method="POST"><input type="hidden" value="'.$users[$i]->GetUserID().'" name="UserID" /><input type="submit" name="EditUser" value="Edit" /></form> <form action="" method="POST"><input type="hidden" value="'.$users[$i]->GetUserID().'" name="UserID" /><input type="submit" name="DeleteUser" value="Delete" /></form></td></tr>';
			}
	
			$xhtml .= '</table>';
		}
		
		return $xhtml;
	}
	
	/**
	 * Shows a edit form for the user.
	 * @param User $user The userinformation to edit.
	 */
	public function doEditUserForm( $user )
	{
		$xhtml = '';
		$xhtml .= '<form action="" method="POST">';
		$xhtml .= '    <input type="hidden" name="EditUserID" value="'.$user->GetUserId().'" />' . "\r\n";
		$xhtml .= '    <label for="username">Username:</label>' . "\n\r";
		$xhtml .= '    <input type="text" name="username" value="'.$user->GetUsername().'" disabled="disabled" />' . "\r\n";
		$xhtml .= '    <label for="oldPassword">Old password:</label>' . "\r\n";
		$xhtml .= '    <input type="password" name="oldPassword" />' . "\r\n";
		$xhtml .= '    <label for="newPassword">New password:</label>' . "\r\n";
		$xhtml .= '    <input type="password" name="newPassword">' . "\r\n";
		$xhtml .= '    <label for="newPasswordMatch">Re-type new password</label>' . "\r\n";
		$xhtml .= '    <input type="password" name="newPasswordMatch" />' . "\r\n";
		$xhtml .= '    <label for="email">E-mail:</label>' . "\r\n";
		$xhtml .= '    <input type="text" name="email" value="'.$user->GetEmail().'" />' . "\r\n";
		$xhtml .= '    <input type="submit" name="SaveEdit" value="Save" /> <input type="submit" name="CancelEdit" value="Cancel" />' . "\r\n";
		$xhtml .= '    </form>' . "\r\n";
		
		return $xhtml;
	}
	
	/**
	* Shows a register form.
	* @return HTML A Register form.
	*/
	public function doRegisterForm()
	{
		$xhtml = '';
		$xhtml = '<form action="" method="POST">
			<label for="username">Username:</label><br />
			<input type="text" name="username" /><br />
			<label for="password">Password:</label><br />
			<input type="password" name="password" /> <br />
			<label for="retypePassword">Re-type password</label><br />
			<input type="password" name="retypePassword" /><br />
			<input type="submit" name="create" value="Create" />
		</form>';
		
		return $xhtml;
	}
	
	// ------------------------------------------------- //
	// EVENTS //
	
	/**
	 * Checks if the POST DeleteUser is set.
	 * @return Bool True/False if it is set.
	 */
	public function DeleteUser()
	{
		if( isset ( $_POST['DeleteUser'] ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * Checks if the POST EditUser is set.
	 * @return Bool True/False if it is set.
	 */
	public function EditUser()
	{
		if( isset ( $_POST['EditUser'] ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * Gets the UserID from POST.
	 * @return Int Returns the UserID from the hidden field.
	 */
	 public function GetPostUserID()
	 {
		 if ( isset ( $_POST['UserID'] ) )
		 {
			 return $_POST['UserID'];
		 }
	 }
	 
	 /**
	 * Checks if the user clicked on Cancel.
	 * @return Bool True/False depending on if the user clicked Cancel.
	 */
	 public function Cancel()
	 {
		 if( isset ( $_POST['CancelEdit'] ) )
		 {
			 return true;
		 }
		 return false;
	 }
	 
	 /**
	 * Checks if the user clicked on Save.
	 * @return Bool true/false if the user clicked save or not.
	 */
	 public function Save()
	 {
		 if( isset ( $_POST['SaveEdit'] ) )
		 {
			 return true;
		 }
		 return false;
	 }
	 
	
	// ------------------------------------------------ //
	// VALIDATION
	
	// TODO: Validate password, username, email.
	public function ValidatePassword()
	{
	}
	
	/**
	* Checks if the new password matches.
	* @param string $newPassword The new password.
	* @param string $newPasswordMatch The new password filed to match.
	* @return Bool If the passwords match, else false.
	*/
	public function newPasswordMatch( $newPassword, $newPasswordMatch )
	{
		if( isset ( $newPassword ) && isset ( $newPasswordMatch ) )
		{
			if( $newPassword === $newPasswordMatch )
			{
				return true;
			}
		}
		
		return false;
	}
}